package ca.camosun.proximity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class DeviceTracker implements Iterable<SavedPerson>{
	private List<SavedPerson> deviceList;
	private String name;
	
	public DeviceTracker(String aName) {
		name = aName;
		deviceList = new ArrayList<SavedPerson>();
	}

	public void addDevice(SavedPerson aDevice) {
		deviceList.add(aDevice);
	}
	
	public SavedPerson getDeviceByIndex(int index){
		 SavedPerson theDevice = deviceList.get(index); 
		
		return theDevice;
	}

	public void removeDevice(SavedPerson aDevice) {
		deviceList.remove(aDevice);
	}
	
	public String getName() {
		return name;
	}
	
	public int deviceCount() {
		return deviceList.size();
	}
	
	public int getDevicePosition(SavedPerson aDevice) {
		return deviceList.indexOf(aDevice);
	}
	
	public String[] listOfDevices() {
		String[] theList = new String[deviceList.size()];
		int i = 0;
		while (i < deviceList.size()) {
			theList[i] = deviceList.get(i).getName();
			i++;
		}
		return theList;
	}
	
	public boolean contains(SavedPerson aPerson) {
		if (deviceList.indexOf(aPerson) == -1) {
			return false;
		}
		return true;
	}

	@Override
	public Iterator<SavedPerson> iterator() {
		Collections.sort(deviceList);
		
		return deviceList.iterator();
	}
	
	public Iterator<SavedPerson> iteratorByName() {
		Collections.sort(deviceList, SavedPerson.byDeviceName());
		
		return deviceList.iterator();
	}

	//temp fix for AlertVibrate's contains function call
	public boolean contains(Person target) {
		if (deviceList.indexOf(target) == -1) {
			return false;
		}
		return true;
	}
	
}